-
Notifications
You must be signed in to change notification settings - Fork 502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix incorrect pseudocode for #[repr(C)] struct alignment #766
Fix incorrect pseudocode for #[repr(C)] struct alignment #766
Conversation
Neat! I'll nix mine. |
@rkanati I assumed that the fact that the original PR stalled meant that you weren't likely to show up, I kinda wanted you to be able to fix yours and get the credit 😭 But I also wanted it done, so I guess I haven't really done anything wrong XD |
No problem! It's just a doc fix, after all. |
I agree now that it's better not to use However, could you please define |
|
||
struct[field].offset = current_offset; | ||
|
||
current_offset += field.size; | ||
} | ||
|
||
struct.size = current_offset + current_offset % struct.alignment; | ||
struct.size = current_offset + padding_needed_for(current_offset, struct.alignment); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rkanati you didn't touch this one in your PR... do you remember why? Was that an oversight, or was the original code actually correct for some reason?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, that was just an oversight.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, too. It would just have been good to get explicit confirmation.
@RalfJung Yeah, I started including a definition of the function but wasn't sure whether that would be necessary. Luckily now that I know about |
Okay so, for reference, here's the code for pub fn padding_needed_for(&self, align: usize) -> usize {
let len = self.size();
// Rounded up value is:
// len_rounded_up = (len + align - 1) & !(align - 1);
// and then we return the padding difference: `len_rounded_up - len`.
//
// We use modular arithmetic throughout:
//
// 1. align is guaranteed to be > 0, so align - 1 is always
// valid.
//
// 2. `len + align - 1` can overflow by at most `align - 1`,
// so the &-mask with `!(align - 1)` will ensure that in the
// case of overflow, `len_rounded_up` will itself be 0.
// Thus the returned padding, when added to `len`, yields 0,
// which trivially satisfies the alignment `align`.
//
// (Of course, attempts to allocate blocks of memory whose
// size and padding overflow in the above manner should cause
// the allocator to yield an error anyway.)
let len_rounded_up = len.wrapping_add(align).wrapping_sub(1)
& !align.wrapping_sub(1);
len_rounded_up.wrapping_sub(len)
} I'm feeling like this might be a bit overkill to directly adapt into what's supposed to be pseudocode. @RalfJung do you think I should use this directly for the definition of I'm actually having a bit of trouble fully understanding what's going on in that code, what with all the layered modular arithmetic and bitwise operations (I'm not really fully acquainted with the identities and such to be able to easily follow what bitwise operations are doing, so after puzzling it out a bit it's turning out to be extremely non-obvious to me how to translate that into a simple presentation of the algorithm suited to pseudocode). Maybe @pnkfelix can help since they wrote that code? |
I was actually thinking of using the implementation from the other PR:
Maybe also add a note somewhere that we are ignoring overflow concerns here, and link to |
@RalfJung Yeah, I'll do that. I'm definitely overthinking this, heh. |
@RalfJung I added a function definition to the pseudocode as requested. I agree that the note directing the reader to |
What do you mean, "warning"? What would it warn about? |
Oh you mean, the fact that we ignore overflows. Yeah seems best to me to call it a warning, we don't want people to copy-paste this code. |
@RalfJung Any further concerns with this PR? 🙂 (Besides the need to squash these commits before merging.) |
So the code looks all right to me now, but I'd prefer to get this confirmed by some data layout guru... summoning @gnzlbg @hanna-kruppe @eddyb |
99ee27b
to
684d54c
Compare
684d54c
to
5d5b233
Compare
@RalfJung All your review comments should be addressed now 🙂 I just edited the existing commits, since the requested changes were pretty minor. |
Thanks, I think they are. :) Now we are just waiting for some extra eyes on this. |
Looks fine to me too, thanks @kinseytamsin! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we are settled then.
Update books ## nomicon 7 commits in 3e6e1001dc6e095dbd5c88005e80969f60e384e1..71241f403091e021842ca8275740e44d0ab0ece1 2019-12-14 22:08:52 +0000 to 2020-02-17 17:21:36 +0100 - (minor) Add backticks around type names (rust-lang/nomicon#197) - Add book.toml (rust-lang/nomicon#185) - Rename `Alloc` to `AllocRef` (rust-lang/nomicon#188) - Lifetimes: explain how to fix destructor example (rust-lang/nomicon#195) - mention soundness (rust-lang/nomicon#194) - Fix example in FFI section Calling Rust from C (rust-lang/nomicon#193) - Removed repeated word (rust-lang/nomicon#191) ## reference 8 commits in 64239df..559e09c 2020-02-10 19:05:13 +0100 to 2020-03-02 01:17:14 +0100 - Syntax error fix (rust-lang/reference#769) - Fix incorrect pseudocode for #[repr(C)] struct alignment (rust-lang/reference#766) - Replace "Field-Less" with "Fieldless" (rust-lang/reference#768) - Removed repeated word (rust-lang/reference#767) - Update for change in const lint name. (rust-lang/reference#764) - semantic type -> resolved type (rust-lang/reference#761) - add behavior change of relative paths without `self` in 2018 edition (rust-lang/reference#757) - assignment operator expressions -> compound assignment expressions (rust-lang/reference#759) ## rust-by-example 3 commits in 32facd5522ddbbf37baf01e4e4b6562bc55c071a..db57f899ea2a56a544c8d280cbf033438666273d 2020-02-11 09:25:06 -0300 to 2020-02-18 17:46:46 -0300 - Minor typo fix in src/mod/visibility (rust-lang/rust-by-example#1309) - Don't suggest Into implements a reverse conversion (rust-lang/rust-by-example#1307) - Improve grammar in example of 'in let' section (rust-lang/rust-by-example#1308) ## embedded-book 2 commits in b2e1092bf67bd4d7686c4553f186edbb7f5f92db..b81ffb7a6f4c5aaed92786e770e99db116aa4ebd 2020-01-30 08:45:46 +0000 to 2020-02-27 08:06:04 +0000 - Setting output to `high` needs a `true` argument (rust-embedded/book#227) - Add licence notes to index.md (rust-embedded/book#226)
Update books ## nomicon 7 commits in 3e6e1001dc6e095dbd5c88005e80969f60e384e1..71241f403091e021842ca8275740e44d0ab0ece1 2019-12-14 22:08:52 +0000 to 2020-02-17 17:21:36 +0100 - (minor) Add backticks around type names (rust-lang/nomicon#197) - Add book.toml (rust-lang/nomicon#185) - Rename `Alloc` to `AllocRef` (rust-lang/nomicon#188) - Lifetimes: explain how to fix destructor example (rust-lang/nomicon#195) - mention soundness (rust-lang/nomicon#194) - Fix example in FFI section Calling Rust from C (rust-lang/nomicon#193) - Removed repeated word (rust-lang/nomicon#191) ## reference 8 commits in 64239df..559e09c 2020-02-10 19:05:13 +0100 to 2020-03-02 01:17:14 +0100 - Syntax error fix (rust-lang/reference#769) - Fix incorrect pseudocode for #[repr(C)] struct alignment (rust-lang/reference#766) - Replace "Field-Less" with "Fieldless" (rust-lang/reference#768) - Removed repeated word (rust-lang/reference#767) - Update for change in const lint name. (rust-lang/reference#764) - semantic type -> resolved type (rust-lang/reference#761) - add behavior change of relative paths without `self` in 2018 edition (rust-lang/reference#757) - assignment operator expressions -> compound assignment expressions (rust-lang/reference#759) ## rust-by-example 3 commits in 32facd5522ddbbf37baf01e4e4b6562bc55c071a..db57f899ea2a56a544c8d280cbf033438666273d 2020-02-11 09:25:06 -0300 to 2020-02-18 17:46:46 -0300 - Minor typo fix in src/mod/visibility (rust-lang/rust-by-example#1309) - Don't suggest Into implements a reverse conversion (rust-lang/rust-by-example#1307) - Improve grammar in example of 'in let' section (rust-lang/rust-by-example#1308) ## embedded-book 2 commits in b2e1092bf67bd4d7686c4553f186edbb7f5f92db..b81ffb7a6f4c5aaed92786e770e99db116aa4ebd 2020-01-30 08:45:46 +0000 to 2020-02-27 08:06:04 +0000 - Setting output to `high` needs a `true` argument (rust-embedded/book#227) - Add licence notes to index.md (rust-embedded/book#226)
Update books ## nomicon 8 commits in 3e6e1001dc6e095dbd5c88005e80969f60e384e1..9f797e65e6bcc79419975b17aff8e21c9adc039f 2019-12-14 22:08:52 +0000 to 2020-03-04 03:45:13 +0100 - Rustup to latest rustc (rust-lang/nomicon#203) - (minor) Add backticks around type names (rust-lang/nomicon#197) - Add book.toml (rust-lang/nomicon#185) - Rename `Alloc` to `AllocRef` (rust-lang/nomicon#188) - Lifetimes: explain how to fix destructor example (rust-lang/nomicon#195) - mention soundness (rust-lang/nomicon#194) - Fix example in FFI section Calling Rust from C (rust-lang/nomicon#193) - Removed repeated word (rust-lang/nomicon#191) ## reference 8 commits in 64239df..559e09c 2020-02-10 19:05:13 +0100 to 2020-03-02 01:17:14 +0100 - Syntax error fix (rust-lang/reference#769) - Fix incorrect pseudocode for #[repr(C)] struct alignment (rust-lang/reference#766) - Replace "Field-Less" with "Fieldless" (rust-lang/reference#768) - Removed repeated word (rust-lang/reference#767) - Update for change in const lint name. (rust-lang/reference#764) - semantic type -> resolved type (rust-lang/reference#761) - add behavior change of relative paths without `self` in 2018 edition (rust-lang/reference#757) - assignment operator expressions -> compound assignment expressions (rust-lang/reference#759) ## rust-by-example 3 commits in 32facd5522ddbbf37baf01e4e4b6562bc55c071a..db57f899ea2a56a544c8d280cbf033438666273d 2020-02-11 09:25:06 -0300 to 2020-02-18 17:46:46 -0300 - Minor typo fix in src/mod/visibility (rust-lang/rust-by-example#1309) - Don't suggest Into implements a reverse conversion (rust-lang/rust-by-example#1307) - Improve grammar in example of 'in let' section (rust-lang/rust-by-example#1308) ## embedded-book 2 commits in b2e1092bf67bd4d7686c4553f186edbb7f5f92db..b81ffb7a6f4c5aaed92786e770e99db116aa4ebd 2020-01-30 08:45:46 +0000 to 2020-02-27 08:06:04 +0000 - Setting output to `high` needs a `true` argument (rust-embedded/book#227) - Add licence notes to index.md (rust-embedded/book#226)
Update books ## nomicon 8 commits in 3e6e1001dc6e095dbd5c88005e80969f60e384e1..9f797e65e6bcc79419975b17aff8e21c9adc039f 2019-12-14 22:08:52 +0000 to 2020-03-04 03:45:13 +0100 - Rustup to latest rustc (rust-lang/nomicon#203) - (minor) Add backticks around type names (rust-lang/nomicon#197) - Add book.toml (rust-lang/nomicon#185) - Rename `Alloc` to `AllocRef` (rust-lang/nomicon#188) - Lifetimes: explain how to fix destructor example (rust-lang/nomicon#195) - mention soundness (rust-lang/nomicon#194) - Fix example in FFI section Calling Rust from C (rust-lang/nomicon#193) - Removed repeated word (rust-lang/nomicon#191) ## reference 8 commits in 64239df..559e09c 2020-02-10 19:05:13 +0100 to 2020-03-02 01:17:14 +0100 - Syntax error fix (rust-lang/reference#769) - Fix incorrect pseudocode for #[repr(C)] struct alignment (rust-lang/reference#766) - Replace "Field-Less" with "Fieldless" (rust-lang/reference#768) - Removed repeated word (rust-lang/reference#767) - Update for change in const lint name. (rust-lang/reference#764) - semantic type -> resolved type (rust-lang/reference#761) - add behavior change of relative paths without `self` in 2018 edition (rust-lang/reference#757) - assignment operator expressions -> compound assignment expressions (rust-lang/reference#759) ## rust-by-example 3 commits in 32facd5522ddbbf37baf01e4e4b6562bc55c071a..db57f899ea2a56a544c8d280cbf033438666273d 2020-02-11 09:25:06 -0300 to 2020-02-18 17:46:46 -0300 - Minor typo fix in src/mod/visibility (rust-lang/rust-by-example#1309) - Don't suggest Into implements a reverse conversion (rust-lang/rust-by-example#1307) - Improve grammar in example of 'in let' section (rust-lang/rust-by-example#1308) ## embedded-book 2 commits in b2e1092bf67bd4d7686c4553f186edbb7f5f92db..b81ffb7a6f4c5aaed92786e770e99db116aa4ebd 2020-01-30 08:45:46 +0000 to 2020-02-27 08:06:04 +0000 - Setting output to `high` needs a `true` argument (rust-embedded/book#227) - Add licence notes to index.md (rust-embedded/book#226)
Update books ## nomicon 8 commits in 3e6e1001dc6e095dbd5c88005e80969f60e384e1..9f797e65e6bcc79419975b17aff8e21c9adc039f 2019-12-14 22:08:52 +0000 to 2020-03-04 03:45:13 +0100 - Rustup to latest rustc (rust-lang/nomicon#203) - (minor) Add backticks around type names (rust-lang/nomicon#197) - Add book.toml (rust-lang/nomicon#185) - Rename `Alloc` to `AllocRef` (rust-lang/nomicon#188) - Lifetimes: explain how to fix destructor example (rust-lang/nomicon#195) - mention soundness (rust-lang/nomicon#194) - Fix example in FFI section Calling Rust from C (rust-lang/nomicon#193) - Removed repeated word (rust-lang/nomicon#191) ## reference 8 commits in 64239df..559e09c 2020-02-10 19:05:13 +0100 to 2020-03-02 01:17:14 +0100 - Syntax error fix (rust-lang/reference#769) - Fix incorrect pseudocode for #[repr(C)] struct alignment (rust-lang/reference#766) - Replace "Field-Less" with "Fieldless" (rust-lang/reference#768) - Removed repeated word (rust-lang/reference#767) - Update for change in const lint name. (rust-lang/reference#764) - semantic type -> resolved type (rust-lang/reference#761) - add behavior change of relative paths without `self` in 2018 edition (rust-lang/reference#757) - assignment operator expressions -> compound assignment expressions (rust-lang/reference#759) ## rust-by-example 3 commits in 32facd5522ddbbf37baf01e4e4b6562bc55c071a..db57f899ea2a56a544c8d280cbf033438666273d 2020-02-11 09:25:06 -0300 to 2020-02-18 17:46:46 -0300 - Minor typo fix in src/mod/visibility (rust-lang/rust-by-example#1309) - Don't suggest Into implements a reverse conversion (rust-lang/rust-by-example#1307) - Improve grammar in example of 'in let' section (rust-lang/rust-by-example#1308) ## embedded-book 2 commits in b2e1092bf67bd4d7686c4553f186edbb7f5f92db..b81ffb7a6f4c5aaed92786e770e99db116aa4ebd 2020-01-30 08:45:46 +0000 to 2020-02-27 08:06:04 +0000 - Setting output to `high` needs a `true` argument (rust-embedded/book#227) - Add licence notes to index.md (rust-embedded/book#226)
Update books ## nomicon 8 commits in 3e6e1001dc6e095dbd5c88005e80969f60e384e1..9f797e65e6bcc79419975b17aff8e21c9adc039f 2019-12-14 22:08:52 +0000 to 2020-03-04 03:45:13 +0100 - Rustup to latest rustc (rust-lang/nomicon#203) - (minor) Add backticks around type names (rust-lang/nomicon#197) - Add book.toml (rust-lang/nomicon#185) - Rename `Alloc` to `AllocRef` (rust-lang/nomicon#188) - Lifetimes: explain how to fix destructor example (rust-lang/nomicon#195) - mention soundness (rust-lang/nomicon#194) - Fix example in FFI section Calling Rust from C (rust-lang/nomicon#193) - Removed repeated word (rust-lang/nomicon#191) ## reference 8 commits in 64239df..559e09c 2020-02-10 19:05:13 +0100 to 2020-03-02 01:17:14 +0100 - Syntax error fix (rust-lang/reference#769) - Fix incorrect pseudocode for #[repr(C)] struct alignment (rust-lang/reference#766) - Replace "Field-Less" with "Fieldless" (rust-lang/reference#768) - Removed repeated word (rust-lang/reference#767) - Update for change in const lint name. (rust-lang/reference#764) - semantic type -> resolved type (rust-lang/reference#761) - add behavior change of relative paths without `self` in 2018 edition (rust-lang/reference#757) - assignment operator expressions -> compound assignment expressions (rust-lang/reference#759) ## rust-by-example 3 commits in 32facd5522ddbbf37baf01e4e4b6562bc55c071a..db57f899ea2a56a544c8d280cbf033438666273d 2020-02-11 09:25:06 -0300 to 2020-02-18 17:46:46 -0300 - Minor typo fix in src/mod/visibility (rust-lang/rust-by-example#1309) - Don't suggest Into implements a reverse conversion (rust-lang/rust-by-example#1307) - Improve grammar in example of 'in let' section (rust-lang/rust-by-example#1308) ## embedded-book 2 commits in b2e1092bf67bd4d7686c4553f186edbb7f5f92db..b81ffb7a6f4c5aaed92786e770e99db116aa4ebd 2020-01-30 08:45:46 +0000 to 2020-02-27 08:06:04 +0000 - Setting output to `high` needs a `true` argument (rust-embedded/book#227) - Add licence notes to index.md (rust-embedded/book#226)
Update books ## nomicon 8 commits in 3e6e1001dc6e095dbd5c88005e80969f60e384e1..9f797e65e6bcc79419975b17aff8e21c9adc039f 2019-12-14 22:08:52 +0000 to 2020-03-04 03:45:13 +0100 - Rustup to latest rustc (rust-lang/nomicon#203) - (minor) Add backticks around type names (rust-lang/nomicon#197) - Add book.toml (rust-lang/nomicon#185) - Rename `Alloc` to `AllocRef` (rust-lang/nomicon#188) - Lifetimes: explain how to fix destructor example (rust-lang/nomicon#195) - mention soundness (rust-lang/nomicon#194) - Fix example in FFI section Calling Rust from C (rust-lang/nomicon#193) - Removed repeated word (rust-lang/nomicon#191) ## reference 8 commits in 64239df..559e09c 2020-02-10 19:05:13 +0100 to 2020-03-02 01:17:14 +0100 - Syntax error fix (rust-lang/reference#769) - Fix incorrect pseudocode for #[repr(C)] struct alignment (rust-lang/reference#766) - Replace "Field-Less" with "Fieldless" (rust-lang/reference#768) - Removed repeated word (rust-lang/reference#767) - Update for change in const lint name. (rust-lang/reference#764) - semantic type -> resolved type (rust-lang/reference#761) - add behavior change of relative paths without `self` in 2018 edition (rust-lang/reference#757) - assignment operator expressions -> compound assignment expressions (rust-lang/reference#759) ## rust-by-example 3 commits in 32facd5522ddbbf37baf01e4e4b6562bc55c071a..db57f899ea2a56a544c8d280cbf033438666273d 2020-02-11 09:25:06 -0300 to 2020-02-18 17:46:46 -0300 - Minor typo fix in src/mod/visibility (rust-lang/rust-by-example#1309) - Don't suggest Into implements a reverse conversion (rust-lang/rust-by-example#1307) - Improve grammar in example of 'in let' section (rust-lang/rust-by-example#1308) ## embedded-book 2 commits in b2e1092bf67bd4d7686c4553f186edbb7f5f92db..b81ffb7a6f4c5aaed92786e770e99db116aa4ebd 2020-01-30 08:45:46 +0000 to 2020-02-27 08:06:04 +0000 - Setting output to `high` needs a `true` argument (rust-embedded/book#227) - Add licence notes to index.md (rust-embedded/book#226)
Update books ## nomicon 8 commits in 3e6e1001dc6e095dbd5c88005e80969f60e384e1..9f797e65e6bcc79419975b17aff8e21c9adc039f 2019-12-14 22:08:52 +0000 to 2020-03-04 03:45:13 +0100 - Rustup to latest rustc (rust-lang/nomicon#203) - (minor) Add backticks around type names (rust-lang/nomicon#197) - Add book.toml (rust-lang/nomicon#185) - Rename `Alloc` to `AllocRef` (rust-lang/nomicon#188) - Lifetimes: explain how to fix destructor example (rust-lang/nomicon#195) - mention soundness (rust-lang/nomicon#194) - Fix example in FFI section Calling Rust from C (rust-lang/nomicon#193) - Removed repeated word (rust-lang/nomicon#191) ## reference 8 commits in 64239df..559e09c 2020-02-10 19:05:13 +0100 to 2020-03-02 01:17:14 +0100 - Syntax error fix (rust-lang/reference#769) - Fix incorrect pseudocode for #[repr(C)] struct alignment (rust-lang/reference#766) - Replace "Field-Less" with "Fieldless" (rust-lang/reference#768) - Removed repeated word (rust-lang/reference#767) - Update for change in const lint name. (rust-lang/reference#764) - semantic type -> resolved type (rust-lang/reference#761) - add behavior change of relative paths without `self` in 2018 edition (rust-lang/reference#757) - assignment operator expressions -> compound assignment expressions (rust-lang/reference#759) ## rust-by-example 3 commits in 32facd5522ddbbf37baf01e4e4b6562bc55c071a..db57f899ea2a56a544c8d280cbf033438666273d 2020-02-11 09:25:06 -0300 to 2020-02-18 17:46:46 -0300 - Minor typo fix in src/mod/visibility (rust-lang/rust-by-example#1309) - Don't suggest Into implements a reverse conversion (rust-lang/rust-by-example#1307) - Improve grammar in example of 'in let' section (rust-lang/rust-by-example#1308) ## embedded-book 2 commits in b2e1092bf67bd4d7686c4553f186edbb7f5f92db..b81ffb7a6f4c5aaed92786e770e99db116aa4ebd 2020-01-30 08:45:46 +0000 to 2020-02-27 08:06:04 +0000 - Setting output to `high` needs a `true` argument (rust-embedded/book#227) - Add licence notes to index.md (rust-embedded/book#226)
Alternative to #559 incorporating some suggested changes to that PR.
@RalfJung I felt like your pseudocode approached actual code too much, and sorta requires understanding what's going on with
Layout
in order to follow, so I just took the route of making the padding calculation into an implicit function per other suggestions.